package servlet;

import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;
import util.Util;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Locale;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String userName=request.getParameter("name");
        String userId=request.getParameter("id");
        String birth=request.getParameter("birth");
        String sex=request.getParameter("sex");
        String address=request.getParameter("address");
        String userPassword=request.getParameter("pwd");
        String rePassword=request.getParameter("repwd");
        String source=request.getParameter("source");
        User user=new User(); //用户信息包装到User
        UserService userService=new UserServiceImpl();
        if(!userPassword.equals(rePassword)){
            request.setAttribute("pwdFalse","pwdFalse"); //密码不一致信息
            if(source!=null){
                request.getRequestDispatcher("addUser.jsp").forward(request,response);
            }else{
                request.getRequestDispatcher("register.jsp").forward(request,response);
            }
        }
        else{
            if(userService.queryStudentById(userId)==null){
                user.setUserId(userId);
                user.setUserName(userName);
                user.setAddress(address);
                user.setRegdate(Util.getDate());
                user.setBirth(birth);
                user.setType("用户");
                user.setUserPassword(userPassword);
                if(sex.toUpperCase(Locale.ROOT).equals("BOY")){
                    user.setSex("Boy");
                }else{
                    user.setSex("Girl");
                }
                if(userService.addUser(user)){
                    response.getWriter().print("注册成功！1s后返回！");
                    if(source!=null){
                        response.setHeader("refresh","1;url=QueryUsersByPageServlet");
                    }else{
                        response.setHeader("refresh","1;url=login.jsp");
                    }
                }else{
                    response.getWriter().print("注册失败！1s后返回！");
                    if(source!=null){
                        request.getRequestDispatcher("addUser.jsp").forward(request,response);
                    }else{
                        response.setHeader("refresh","1;url=register.jsp");
                    }
                }
            }else{
                request.setAttribute("userHasExist","userHasExist"); //账户存在信息
                if(source!=null){
                    request.getRequestDispatcher("addUser.jsp").forward(request,response);
                }else{
                    request.getRequestDispatcher("register.jsp").forward(request,response);
                }
            }
        }
    }
}
